﻿using System;
using System.Linq;
using Infrastructure;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.Interface;


namespace OpenAuth.App
{
    public class OUTP_FZ_YSDLXXApp : TriageBaseApp<OUTP_FZ_YSDLXX>
    {
        private RevelanceManagerApp _revelanceApp;
        private IAuth _auth;

        /// <summary>
        /// 加载列表
        /// </summary>
        //public TableData Load(QueryOUTP_FZ_YSDLXXListReq request)
        //{
        //    var loginContext = _auth.GetCurrentUser();
        //    if (loginContext == null)
        //    {
        //        throw new CommonException("登录已过期", Define.INVALID_TOKEN);
        //    }

        //    var properties = loginContext.GetProperties("OUTP_FZ_YSDLXX");

        //    if (properties == null || properties.Count == 0)
        //    {
        //        throw new Exception("当前登录用户没有访问该模块字段的权限，请联系管理员配置");
        //    }


        //    var result = new TableData();
        //    var objs = Repository.Find(null);
        //    if (!string.IsNullOrEmpty(request.key))
        //    {
        //        objs = objs.Where(u => u.Id.Contains(request.key));
        //    }


        //    var propertyStr = string.Join(',', properties.Select(u => u.Key));
        //    result.columnHeaders = properties;
        //    result.data = objs.OrderBy(u => u.Id)
        //        .Skip((request.page - 1) * request.limit)
        //        .Take(request.limit).Select($"new ({propertyStr})");
        //    result.count = objs.Count();
        //    return result;
        //}

        public void Add(AddOrUpdateOUTP_FZ_YSDLXXReq req)
        {
            var obj = req.MapTo<OUTP_FZ_YSDLXX>();
            //todo:补充或调整自己需要的字段
            //obj.CreateTime = DateTime.Now;
            //var user = _auth.GetCurrentUser().User;
            //obj.CreateUserId = user.Id;
            //obj.CreateUserName = user.Name;
            Repository.Add(obj);
        }

        // public void Update(AddOrUpdateOUTP_FZ_YSDLXXReq obj)
        //{
        //    var user = _auth.GetCurrentUser().User;
        //    Repository.Update(u => u.Id == obj.Id, u => new OUTP_FZ_YSDLXX
        //    {
        //        KSMC = obj.KSMC,
        //        ZJYSDM = obj.ZJYSDM,
        //        ZJYSMC = obj.ZJYSMC,
        //        IPDZ = obj.IPDZ,
        //        WKDZ = obj.WKDZ,
        //        ZJMC = obj.ZJMC,
        //        DLZT = obj.DLZT,
        //        ZJBZ = obj.ZJBZ,
        //        SEX = obj.SEX,
        //        YYPBBZ = obj.YYPBBZ,
        //        KMDM = obj.KMDM,
        //        KMMC = obj.KMMC,
        //        //UpdateTime = DateTime.Now,
        //        //UpdateUserId = user.Id,
        //        //UpdateUserName = user.Name
        //        //todo:补充或调整自己需要的字段
        //    });

        //}
        /// <summary>
        /// 在OUTP_FZ_YSDLXX表中获取当班信息 
        /// </summary>
        /// <param name="ysdm">医生代码</param>
        /// <returns></returns>
        public OUTP_FZ_YSDLXX QuerySingle(string ysdm)
        {
            string date = DateTime.Now.ToString("yyyyMMdd");
            return Repository.FindSingle(u => u.ZJYSDM == ysdm&&u.DLSJ.StartsWith(date)&&u.DLZT==true);
        }
            
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="app"></param>
        /// <param name="auth"></param>
        public OUTP_FZ_YSDLXXApp(ITriageRepository<OUTP_FZ_YSDLXX> repository,
            RevelanceManagerApp app, IAuth auth) : base( repository)
        {
            _revelanceApp = app;
            _auth = auth;
        }
    }
}